package de.baumann.browser.present

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.LoginInfo
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IHashRankingView
import de.baumann.browser.model.MiningModel

class LeaderboardPresenter : BasePresenter<IHashRankingView>() {
    fun getHashRanking() {
        MiningModel().hashRanking
                .subscribe(object : OdinObserver<List<LoginInfo>>(view) {
                    override fun onSuccess(data: List<LoginInfo>?) {
                        if (data == null || view == null) {
                            return
                        }

                        if (data.size == 1) {
                            view.setChampion(data[0])
                            return
                        }

                        if (data.size == 2) {
                            view.setChampion(data[0])
                            view.setSecondPlace(data[1])
                            return
                        }

                        if (data.size >= 3) {
                            view.setChampion(data[0])
                            view.setSecondPlace(data[1])
                            view.setThirdPlace(data[2])
                            if (data.size > 3) {
                                view.setRankingList(data.subList(3, data.size))
                            }
                        }
                    }
                })
    }
}